package com.cloud.pole.user.controller;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.api.user.UserAccountApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.user.biz.BaseUserBiz;
import com.cloud.pole.user.model.entity.BaseUser;
import com.cloud.pole.user.model.vo.account.UserAccountDetailVo;
import com.cloud.pole.user.model.vo.account.UserAccountVo;
import com.cloud.pole.user.service.account.UserAccountDetailService;
import com.cloud.pole.user.service.account.UserAccountService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("userAccount/front")
@Slf4j
public class UserAccountController{

	@Autowired
	UserAccountService userAccountService;
	
	@ResponseBody
    @RequestMapping(value = "/getUserAccountList")
    public ApiRestResponse<List<UserAccountApi>> getUserAccountList(
    		@RequestHeader(defaultValue = "") String token){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userAccountService.getAccountList(token));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
    @RequestMapping(value = "/getUserAccount")
    public ApiRestResponse<UserAccountVo> getUserAccount(
    		@RequestHeader(defaultValue = "") String token, 
    		@RequestParam(defaultValue = "") String accountId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(accountId), "accountId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(accountId), "accountId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userAccountService.getUserAccount(token, Long.parseLong(accountId)));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
